<?php

declare(strict_types=1);

namespace app\api\controller\v1;

use app\api\BaseApiController;
use app\system\model\Dept;
use app\admin\model\Admin;

class Contact extends BaseApiController
{
    /**
     * 部门与成员
     * url: http://域名/api/v1/contact/users
     * doc: http://lutui.net/doc/app/contacts.html
     */
    public function users()
    {
        $id = $this->data['id'];
        $arr = Dept::where('pid', $id)->order(['sort' => 'asc', 'id' => 'desc'])->select();
        $data = [];
        foreach ($arr as $value) {
            $dept = [
                'id' => intval($value['id']),
                'name' => $value['name'],
                'dept' => 1,
            ];
            if ($value['icon']) {
                $dept['icon'] = $value['icon'];
            } elseif ($value['image']) {
                $dept['icon'] = DOMAIN . $value['image'];
            }
            $data[] = $dept;
        }
        $none = DOMAIN . '/static/images/none.png';
        if ($id > 0) {
            $ids = Dept::getDeptId($id);
            $arr = Admin::where('status', 0)->whereIn('dept_id', $ids)->select();
        } else {
            $arr = Admin::where('status', 0)->select();
        }
        if ($arr) {
            foreach ($arr as $value) {
                $data[] = [
                    'id' => intval($value['id']),
                    'name' => $value['nickname'],
                    'phone' => $value['phone'],
                    'url' => $value['avatar'] ? DOMAIN . $value['avatar'] : $none,
                ];
            }
        }
        return json($data);
    }
}
